#include "CheckInResponsePanel.h"
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QTextEdit>
#include "../../../../../mes/utils/TaskManager.h"
#include "../../../../models/CheckInData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"

QHash<QString,QLineEdit*> responselineList;

QHash<QString,QTextEdit*> jsonEditList;

CheckInResponsePanel::CheckInResponsePanel(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
    task = new TaskManager(BaseData<CommonData>::execute(&CommonData::getNetwork));

    connect(SignalBus::instance(),&SignalBus::sendCheckin,this,&CheckInResponsePanel::refreshData);
}

void CheckInResponsePanel::initWidgets()
{
    const auto lbResultCode = new QLabel(tr("结果代码"));
    const auto txtResultCode = new QLineEdit();
    txtResultCode->setReadOnly(true);


    const auto gridLayout = new QGridLayout();
    const auto lbLocationNo = new QLabel(tr("工位号"));
    const auto txtLocationNo = new QLineEdit();
    const auto lbPcu = new QLabel(tr("产品SN号"));
    const auto txtPcu = new QLineEdit();
    const auto lbResultMsg = new QLabel(tr("结果信息"));
    const auto txtResultMsg = new QLineEdit();

    const auto txtJsonContext = new QTextEdit();

    gridLayout->addWidget(lbLocationNo,0,0,1,1);
    gridLayout->addWidget(txtLocationNo,0,1,1,1);
    gridLayout->addWidget(lbPcu,1,0,1,1);
    gridLayout->addWidget(txtPcu,1,1,1,1);
    gridLayout->addWidget(lbResultMsg,2,0,1,1);
    gridLayout->addWidget(txtResultMsg,2,1,1,1);
    gridLayout->addWidget(lbResultCode,3,0,1,1);
    gridLayout->addWidget(txtResultCode,3,1,1,1);
    gridLayout->addWidget(txtJsonContext,4,0,1,2);
    setLayout(gridLayout);

    responselineList.insert("locationNo",txtLocationNo);
    responselineList.insert("pcu",txtPcu);
    responselineList.insert("resultMsg",txtResultMsg);
    responselineList.insert("resultCode",txtResultCode);

    jsonEditList.insert("jsonContent",txtJsonContext);


}

void CheckInResponsePanel::setupUi()
{
    initWidgets();
}


void CheckInResponsePanel::refreshData()
{

    QString factoryNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo));
    QString machineNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));
    QString locationNo = QString::fromStdString(BaseData<CheckInData>::execute(&CheckInData::getLocationNo));
    QString eventUser = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser));
    QString batch = QString::fromStdString(BaseData<CheckInData>::execute(&CheckInData::getBatch));
    QString pcu = QString::fromStdString(BaseData<CheckInData>::execute(&CheckInData::getPCU));
    QList<BatchCheckInPcuInfo> pcuInfo;
    BatchCheckInPcuInfo item {locationNo,pcu};
    pcuInfo.append(item);
    const auto response = task->BatchCheckIn(factoryNo,eventUser,machineNo,batch,pcuInfo);

    if(response!=nullptr)
    {
        responselineList["resultCode"]->setText(response->resultCode);
        jsonEditList["jsonContent"]->setText(response->jsonContent);

        if(!response->pcuInfo.empty())
        {
            responselineList["locationNo"]->setText(response->pcuInfo[0].locationNo);
            responselineList["pcu"]->setText(response->pcuInfo[0].pcu);
            responselineList["resultMsg"]->setText(response->pcuInfo[0].resultMsg);
            jsonEditList["jsonContent"]->setText(response->errorMsg);
        }
        else
        {
            responselineList["locationNo"]->setText(tr("获取失败"));
            responselineList["pcu"]->setText(tr("获取失败"));
            responselineList["resultMsg"]->setText(tr("获取失败"));
        }
    }
    else
    {
        responselineList["locationNo"]->setText(tr("获取失败"));
        responselineList["pcu"]->setText(tr("获取失败"));
        responselineList["resultCode"]->setText(tr("获取失败"));
        responselineList["resultMsg"]->setText(tr("获取失败"));
    }

    // BaseData<CommonData>::execute(&CommonData::getNetwork)->cancelTask(task->TaskId(task->BatchCheckIn()));
}
